package com.grwx.omc.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.grwx.omc.admin.dao.SysLookupDetailMapper;
import com.grwx.omc.admin.dao.SysLookupTypeMapper;
import com.grwx.omc.admin.entity.SysLookupDetail;
import com.grwx.omc.admin.entity.SysLookupType;
import com.grwx.omc.admin.service.ISysLookupTypeService;
import com.grwx.omc.constant.Constant;
import com.grwx.omc.exception.ValidationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service
public class SysLookupTypeServiceImpl extends ServiceImpl<SysLookupTypeMapper, SysLookupType>
        implements ISysLookupTypeService {

    @Resource
    private SysLookupTypeMapper sysLookupTypeMapper;

    @Resource
    private SysLookupDetailMapper sysLookupDetailMapper;

    @Override
    public IPage<SysLookupType> page(Page<SysLookupType> page, SysLookupType sysLookupType) {
        return sysLookupTypeMapper.pageList(page, sysLookupType);
    }

    @Override
    public void addOrUpdate(SysLookupType sysLookupType) {
        if (null == sysLookupType) {
            throw new ValidationException(Constant.MsgCode.EMPTY_PARAM);
        }
        if (StringUtils.isEmpty(sysLookupType.getParentId())) {
            sysLookupType.setParentId(Constant.LOOKUP_TYPE_ROOT_ID);
        }
        // 字典类型编码唯一性校验
        uniqueCheck(sysLookupType);

        if (null == sysLookupType.getId()) {
            sysLookupType.preInsert();
            save(sysLookupType);
        } else {
            sysLookupType.preUpdate();
            updateById(sysLookupType);
        }
    }

    /**
     * typeCode唯一校验
     * 规则：同一服务(parentId)下，字典类型不能重复
     * @param sysLookupType
     */
    private void uniqueCheck(SysLookupType sysLookupType) {
        QueryWrapper<SysLookupType> wrapper = new QueryWrapper<>();
        if (null != sysLookupType.getParentId()) {
            wrapper.eq("parent_id", sysLookupType.getParentId());
        }
        wrapper.and(Wrapper -> wrapper.eq("type_code", sysLookupType.getTypeCode())
                        .or()
                        .eq("type_name", sysLookupType.getTypeName()));
        List<SysLookupType> existLookupType = list(wrapper);
        if (!ObjectUtils.isEmpty(existLookupType)) {
            throw new ValidationException("omc.admin.lookup_type.E001");
        }
    }


    @Override
    public List<SysLookupType> listRootType() {
        return list(new QueryWrapper<SysLookupType>()
                .eq("parent_id", 0)
                .orderByDesc("update_time"));
    }


    @Override
    @Transactional
    public void del(Long id) {
        removeById(id);
        sysLookupDetailMapper.delete(new QueryWrapper<SysLookupDetail>().eq("parent_id", id));
    }

    @Override
    @Transactional
    public void batchDel(Long[] ids) {
        removeByIds(Arrays.asList(ids));
        sysLookupDetailMapper.delete(new QueryWrapper<SysLookupDetail>().in("parent_id", ids));
    }
}
